﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.oa.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.auth.entity.SystemBranchCompany;
import com.hh.bss.auth.entity.SystemBranchTitle;
import com.hh.bss.auth.entity.SystemUser;
import com.hh.bss.auth.service.SystemBranchCompanyService;
import com.hh.bss.auth.service.SystemUserService;
import com.hh.bss.base.BaseBusinessService;
import com.hh.bss.base.EntityDao;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.SystemConstant;
import com.hh.bss.common.page.Page;
import com.hh.bss.config.Environment;
import com.hh.bss.customer.entity.Company;
import com.hh.bss.customer.entity.Customer;
import com.hh.bss.customer.service.CustomerService;
import com.hh.bss.oa.dao.OaEmailSendDao;
import com.hh.bss.oa.entity.OaEmailSend;
import com.hh.bss.oa.service.OaEmailSendService;
import com.hh.bss.oa.vo.query.OaEmailSendQuery;
import com.hh.bss.project.entity.AccountPn;
import com.hh.bss.project.entity.ContractJob;
import com.hh.bss.project.entity.ContractJobCvInterview;
import com.hh.bss.project.entity.ContractJobCvOffer;
import com.hh.bss.project.entity.ContractJobCvRecom;
import com.hh.bss.project.entity.SearchProjectInterview;
import com.hh.bss.project.service.ContractJobCvRecomService;
import com.hh.bss.project.service.ContractJobService;
import com.hh.bss.sysadmin.entity.SystemTemplate;
import com.hh.bss.sysadmin.service.SystemTemplateService;
import com.hh.bss.talent.entity.Cv;
import com.hh.bss.talent.service.CvService;
import com.hh.bss.util.DateHelper;
import com.hh.bss.util.ObjectUtils;
import com.hh.bss.util.RandomHelper;
import com.hh.bss.util.StringHelper;


@Service
@Transactional
public class OaEmailSendServiceImpl extends BaseBusinessService<OaEmailSend,java.lang.Integer>  implements OaEmailSendService<OaEmailSend,java.lang.Integer>{

	private OaEmailSendDao oaEmailSendDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	private SystemUserService systemUserService;
	private CvService cvService;
	private CustomerService customerService;
	private ContractJobService contractJobService;
	private ContractJobCvRecomService contractJobCvRecomService;
	private SystemBranchCompanyService systemBranchCompanyService;
	private SystemTemplateService systemTemplateService;
	public void setOaEmailSendDaoImpl(OaEmailSendDao dao) {
		this.oaEmailSendDao = dao;
	}
	public void setCustomerServiceImpl(CustomerService service) {
		this.customerService = service;
	}
	public void setCvServiceImpl(CvService service) {
		this.cvService = service;
	}
	public void setContractJobServiceImpl(ContractJobService service) {
		this.contractJobService = service;
	}
	public void setContractJobCvRecomServiceImpl(ContractJobCvRecomService service) {
		this.contractJobCvRecomService = service;
	}
	public void setSystemBranchCompanyServiceImpl(SystemBranchCompanyService service) {
		this.systemBranchCompanyService = service;
	}
	public void setSystemTemplateServiceImpl(SystemTemplateService service) {
		this.systemTemplateService = service;
	}
	public void setSystemUserServiceImpl(SystemUserService service) {
		this.systemUserService = service;
	}
	public EntityDao getEntityDao() {
		return this.oaEmailSendDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(OaEmailSendQuery query) {
		return oaEmailSendDao.findPage(query);
	}
	private String getSign(SystemUser systemUser){
//		罗俊杰 Roger Luo
//
//		唐柏企业咨询有限公司
//		Talent-Bank Consulting Co., Ltd.
//		Shanghai . Beijing . Guangzhou
//		Website: www.talent-bank.cn 
//
//		Mobile   : 
//		Email    : roger.luo@talent-bank.cn
//		Tel      : 
//		Address  : 

		StringBuffer sb=new StringBuffer("<br/>");
		
		SystemBranchCompany systemBranchCompany=null;
		if (systemUser.getBranchCompanyId()!=null)systemBranchCompany=(SystemBranchCompany)systemBranchCompanyService.getById(systemUser.getBranchCompanyId());
		
		
		if (StringHelper.isEmpty(systemUser.getNameCn()))
			sb.append(StringHelper.formatNullValue(systemUser.getNameCn(), "")).append(systemUser.getName()).append("<br />").append("\n");
		else 
		    sb.append(StringHelper.formatNullValue(systemUser.getNameCn(), "")).append("  ").append(systemUser.getName()).append("<br>").append("\n");
		
		if (!StringHelper.isEmpty(systemUser.getTitleName())||!StringHelper.isEmpty(systemUser.getTitleNameEn()))			 
		    sb.append(StringHelper.formatNullValue(systemUser.getTitleName(), "")).append("  ").append(systemUser.getTitleNameEn()).append("<br>").append("\n");
		
		sb.append("<br />").append("\n");;
		sb.append("<span class=chinese>唐柏企业咨询有限公司</span>").append("<br />").append("\n");
		sb.append("Talent-Bank Consulting Co., Ltd.").append("<br />").append("\n");;
		sb.append("Shanghai . Beijing . Guangzhou").append("<br />").append("\n");;
		sb.append("Website: www.tbcg.cn ").append("<br />").append("\n");		
		sb.append("<br />").append("\n");;
		sb.append("Mobile  :").append(StringHelper.formatNullValue(systemUser.getMobile(), "")).append("<br />").append("\n");;
		sb.append("Email  :").append(StringHelper.formatNullValue(systemUser.getEmail(), "")).append("<br />").append("\n");;
		sb.append("Tel  :").append(StringHelper.formatNullValue(systemUser.getTel(), "")).append("<br />").append("\n");;
		sb.append("Address  :").append("\n");;
		
		if (systemBranchCompany!=null) sb.append(systemBranchCompany.getAddress());
		sb.append("<br>").append("\n");;
		return sb.toString();
	}
	public OaEmailSend getCREmail(ContractJobCvRecom contractJobCvRecom,
			OnlineUser onlineUser) {
		//推荐人选邮件
        OaEmailSend oaEmailSend=new OaEmailSend();
		
		StringBuffer sql = new StringBuffer();
		List values = new ArrayList();
		//
		sql.append(" select * from ( ");
		sql.append(" select  a.id,a.job_id jobId,a.cv_id cvId,a.recom_result recomResult,a.recom_risk recomRisk,a.doc_size docSize,a.export_template exportTemplate,a.export_doc exportDoc,a.upload_doc uploadDoc,a.upload_doc_en uploadDocEn,a.is_smsnote isSmsnote,a.status,a.creator,a.create_dt createDt,a.modifier,a.modified_dt modifiedDt,a.summary,b.cv_name cvName,c.name jobName,c.hr_contacter hrContacter,b.email from  contract_job_cv_recom a,cv b,contract_job c where a.cv_id=b.id and a.job_id=c.id and a.id=? " );
		sql.append(" ) t ");
		values.add(contractJobCvRecom.getId());
		
	
		List l= oaEmailSendDao.getJdbcDao().findByQuery(sql.toString(),values.toArray(), new MapObjectRowMapper());
		
		if (l!=null&&l.size()>0){
			
			//取推荐模板 
			SystemTemplate systemTemplate=systemTemplateService.getSystemTemplateByType(ConstantDic.EmailTemplateType_CR);
			
			Map m=(Map)l.get(0);
			String cvName=(String)m.get("cvName");
			String jobName=(String)m.get("jobName");
			String email = (String)m.get("email");
			//String hrContacter = (String)m.get("hrContacter");
			String uploadDoc=(String)m.get("uploadDoc");
			String uploadDocEn=(String)m.get("uploadDocEn");
			oaEmailSend.setCvName(cvName);
			//oaEmailSend.settRecieverName(hrContacter); 
			//oaEmailSend.setRecieverName(hrContacter);
			oaEmailSend.setReciever(email);
			oaEmailSend.setJobName(jobName);
			
			if (!StringHelper.isEmpty(uploadDoc)){
			  oaEmailSend.setAttachment(uploadDoc);
			  String suffix = uploadDoc.substring(uploadDoc.lastIndexOf(".")+1);
			  String fileName= jobName+"_"+cvName+"_"+DateHelper.toString(new Date(), "yyyyMMdd")+"."+suffix;
			  oaEmailSend.setAttachmentName(fileName);
			}
			oaEmailSend.setJobId(ObjectUtils.toString(m.get("jobId")));
			oaEmailSend.setCvId(ObjectUtils.toString(m.get("cvId")));
			
			//oaEmailSend.setAttachmentName(cvName+"_"+jobName+"_"+DateHelper.toString(new Date(), "yyyyMMdd"));
			
			if (!StringHelper.isEmpty(uploadDocEn)){
			  oaEmailSend.setAttachment2(uploadDocEn);
			  String suffix = uploadDocEn.substring(uploadDocEn.lastIndexOf(".")+1);
			  String fileName= jobName+"_"+cvName+"_"+DateHelper.toString(new Date(), "yyyyMMdd(英)")+"."+suffix;
			  oaEmailSend.setAttachment2Name(fileName);
			}
			 
			oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_CR);
			oaEmailSend.setCallId(contractJobCvRecom.getId());
			
			//设置发送人信息
			SystemUser systemUser=null;
			if (onlineUser!=null) {
				systemUser=systemUserService.findByUsername(onlineUser.getUsername());
				if (oaEmailSend!=null&&systemUser!=null){
					oaEmailSend.setSender(systemUser.getEmail());
				}
			}
			
			//oaEmailSend.setSubject("职位\""+(String)m.get("jobName")+"\"人选推荐\""+(String)m.get("cvName")+"\"");
			//recomResult
			//查询职位联系人姓名和邮箱
			
			//List jobContacter=contractJobCvRecomService.getJobContacter((Integer)m.get("jobId"));
			Integer  hrContacter = (Integer)m.get("hrContacter");
			if(hrContacter != null && hrContacter > 0 ){
				Cv cv = cvService.getById(hrContacter);
				if (cv!=null){
					//Map mc=(Map)jobContacter.get(0);
					/*oaEmailSend.setReciever((String)mc.get("email"));
					if(hrContacter == null || "".equals(hrContacter)){
						oaEmailSend.setRecieverName((String)mc.get("cvName"));
						oaEmailSend.settRecieverName((String)mc.get("cvName"));
					}*/
					oaEmailSend.setReciever(cv.getEmail());
					oaEmailSend.setRecieverName(cv.getCvName() == null ? cv.getCvNameEn():cv.getCvName());
					oaEmailSend.settRecieverName(cv.getCvName() == null ? cv.getCvNameEn():cv.getCvName());
					oaEmailSend.setBccreciever(systemUser.getEmail());
				}
			}
			if (systemTemplate!=null){
				Map vm=new HashMap();
				//设置标题
				vm.put("jobName", oaEmailSend.getJobName());
				vm.put("cvName", oaEmailSend.getCvName());
				vm.put("jobContacter", oaEmailSend.getRecieverName());
				
				if (systemUser!=null){
					//vm.put("senderSign",systemUser.getName());
					String sign=getSign(systemUser);
					vm.put("senderSign",sign);
				}
				
				oaEmailSend.setSubject(StringHelper.templateGen(vm, systemTemplate.getTitle()));
				//设置内容
				oaEmailSend.setContent(StringHelper.templateGen(vm, systemTemplate.getContent()));
				
				
			}
			
			
		}
		
			
		return oaEmailSend;
		// TODO Auto-generated method stub
		
	}
	
	public OaEmailSend getPNEmail(AccountPn accountPn, OnlineUser onlineUser) {
		// TODO Auto-generated method stub
		OaEmailSend oaEmailSend=new OaEmailSend();
		AccountPn oldAccountPn=null;
		List l=oaEmailSendDao.getHibernateTemplate().find("from AccountPn t where t.offerId=?", accountPn.getOfferId());
		if (l!=null&&l.size()>0) oldAccountPn= (AccountPn)l.get(0);
		if (oldAccountPn==null) return oaEmailSend;
		ContractJobCvOffer contractJobCvOffer=null;
		l=oaEmailSendDao.getHibernateTemplate().find("from ContractJobCvOffer t where t.id=?", accountPn.getOfferId());
		if (l!=null&&l.size()>0) contractJobCvOffer= (ContractJobCvOffer)l.get(0);
		
		
		ContractJobCvRecom contractJobCvRecom=null;
		if (contractJobCvOffer!=null){
			List recomList= contractJobCvRecomService.getContractJobCvRecomById(contractJobCvOffer.getRecomId());
			
			//取推荐模板 
			SystemTemplate systemTemplate=systemTemplateService.getSystemTemplateByType(ConstantDic.EmailTemplateType_PN);
			
			Map m=(Map)recomList.get(0);
			oaEmailSend.setCvName((String)m.get("cvName"));
			oaEmailSend.setJobName((String)m.get("jobName"));
			oaEmailSend.setAttachment(oldAccountPn.getUploadDoc());
			oaEmailSend.setAttachmentName(oldAccountPn.getUploadDoc());
			oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_PN);
			oaEmailSend.setCallId(accountPn.getOfferId());
			
			//设置发送人信息
			SystemUser systemUser=null;
			if (onlineUser!=null) {
				systemUser=systemUserService.findByUsername(onlineUser.getUsername());
				if (oaEmailSend!=null&&systemUser!=null){
					oaEmailSend.setSender(systemUser.getEmail());
					oaEmailSend.setBccreciever(systemUser.getEmail());
				}
			}
			
			//oaEmailSend.setSubject("职位\""+(String)m.get("jobName")+"\"人选推荐\""+(String)m.get("cvName")+"\"");
			//recomResult
			//查询职位联系人姓名和邮箱
			
			List jobContacter=contractJobCvRecomService.getJobContacter((Integer)m.get("jobId"));
			if (jobContacter!=null&&jobContacter.size()>0){
				
				Map mc=(Map)jobContacter.get(0);
				oaEmailSend.setReciever((String)mc.get("email"));
				oaEmailSend.setRecieverName((String)mc.get("cvName"));
			}
			
			if (systemTemplate!=null){
				Map vm=new HashMap();
				//设置标题
				vm.put("jobName", oaEmailSend.getJobName());
				vm.put("cvName", oaEmailSend.getCvName());
				vm.put("jobContacter", oaEmailSend.getRecieverName());
				if (oldAccountPn!=null)  vm.put("offerPnNo",oldAccountPn.getNbr());
				//if (systemUser!=null)vm.put("senderSign",systemUser.getName());
				if (systemUser!=null){
					//vm.put("senderSign",systemUser.getName());
					String sign=getSign(systemUser);
					vm.put("senderSign",sign);
				}
				oaEmailSend.setSubject(StringHelper.templateGen(vm, systemTemplate.getTitle()));
				//设置内容
				oaEmailSend.setContent(StringHelper.templateGen(vm, systemTemplate.getContent()));
				
			}
		}
		
		
		return oaEmailSend;
	}
	
	public OaEmailSend getConsultantIVEmail(
		  SearchProjectInterview searchProjectInterview, OnlineUser onlineUser) {
		// TODO Auto-generated method stub
		OaEmailSend oaEmailSend=new OaEmailSend();
		SearchProjectInterview oldSearchProjectInterview=null;
		List l=oaEmailSendDao.getHibernateTemplate().find("from SearchProjectInterview t where t.id=?", searchProjectInterview.getId());
		if (l!=null&&l.size()>0) oldSearchProjectInterview= (SearchProjectInterview)l.get(0);
		
		if (oldSearchProjectInterview!=null){
			
			//查询发面试官的电话
			List interviewl=oaEmailSendDao.getHibernateTemplate().find("from SystemUser t where t.account=?", oldSearchProjectInterview.getInterviewer());
			SystemUser interviewSystemUser=null;
			if (interviewl!=null&&interviewl.size()>0) {
				interviewSystemUser= (SystemUser)interviewl.get(0);
				//查询职位
				List l2=oaEmailSendDao.getHibernateTemplate().find("from SystemBranchTitle t where t.id=?", interviewSystemUser.getTitleId());
				SystemBranchTitle systemBranchTitle=null;
				if (l2!=null&&l2.size()>0) {
					systemBranchTitle=(SystemBranchTitle)l2.get(0);
					interviewSystemUser.setTitleName(systemBranchTitle.getName());				
				}
			}
			Map cvMap=cvService.getCvInfoById(oldSearchProjectInterview.getCvId());
			
			oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_GI);
			oaEmailSend.setCallId(oldSearchProjectInterview.getId());
			oaEmailSend.setReciever((String)cvMap.get("email"));
			oaEmailSend.setRecieverName((String)cvMap.get("cvName"));
			
			//查询用户邮箱
			SystemUser systemUser=null;
			if (onlineUser!=null) {
				systemUser=systemUserService.findByUsername(onlineUser.getUsername());
				if (oaEmailSend!=null&&systemUser!=null){
					oaEmailSend.setSender(systemUser.getEmail());
					oaEmailSend.setBccreciever(systemUser.getEmail());
				}
			}
			//取推荐模板 
			SystemTemplate systemTemplate=systemTemplateService.getSystemTemplateByType(ConstantDic.EmailTemplateType_GI);
			if (systemTemplate!=null){
				Map vm=new HashMap();
				//设置标题
				vm.put("interviewDate", DateHelper.toString(oldSearchProjectInterview.getPlanDt(), DateHelper.DEFAULT_DATE_FORMAT));
				vm.put("interviewTime", DateHelper.toString(oldSearchProjectInterview.getPlanDt(), "HH:mm"));
				//vm.put("jobName", oaEmailSend.getJobName());
				vm.put("cvName", (String)cvMap.get("cvName"));
				
				vm.put("mobile", (String)cvMap.get("mobile"));
				vm.put("interviewPlace", oldSearchProjectInterview.getAddress());
				if (interviewSystemUser!=null) {
					vm.put("interviewer", interviewSystemUser.getName());
					vm.put("interviewerTitle", interviewSystemUser.getTitleName());
					//if (StringHelper.isEmpty(interviewSystemUser.getMobile()))						
					vm.put("interviewerPhone", ObjectUtils.toString(interviewSystemUser.getMobile())+" "+ObjectUtils.toString(interviewSystemUser.getTel()));
				}
				
				//if (systemUser!=null)vm.put("senderSign",systemUser.getName());
				if (systemUser!=null){
					//vm.put("senderSign",systemUser.getName());
					String sign=getSign(systemUser);
					vm.put("senderSign",sign);
				}
				oaEmailSend.setSubject(StringHelper.templateGen(vm, systemTemplate.getTitle()));
				//设置内容
				oaEmailSend.setContent(StringHelper.templateGen(vm, systemTemplate.getContent()));
				
				
				
			}
		}
		
		
		return oaEmailSend;
	}
	
	public OaEmailSend getCustomerIVEmail(
			ContractJobCvInterview contractJobCvInterview, OnlineUser onlineUser) {
		// TODO Auto-generated method stub
		
		OaEmailSend oaEmailSend=new OaEmailSend();
		ContractJobCvInterview oldContractJobCvInterview=null;
		List l=oaEmailSendDao.getHibernateTemplate().find("from ContractJobCvInterview t where t.id=?", contractJobCvInterview.getId());
		if (l!=null&&l.size()>0) oldContractJobCvInterview= (ContractJobCvInterview)l.get(0);
		
		if (oldContractJobCvInterview!=null){
			
			//查询职位
			//ContractJob contractJob=(ContractJob)contractJobService.getById(oldContractJobCvInterview.getRecomId());
            List recomList= contractJobCvRecomService.getContractJobCvRecomById(oldContractJobCvInterview.getRecomId());
			
			//取推荐模板 						
			Map m=(Map)recomList.get(0);
			
			Map cvMap=cvService.getCvInfoById((Integer)m.get("cvId"));
			
			oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_KI);
			oaEmailSend.setCallId(oldContractJobCvInterview.getId());
			oaEmailSend.setReciever((String)cvMap.get("email"));
			oaEmailSend.setRecieverName((String)cvMap.get("cvName"));
			
			//查询用户邮箱
			SystemUser systemUser=null;
			if (onlineUser!=null) {
				systemUser=systemUserService.findByUsername(onlineUser.getUsername());
				if (oaEmailSend!=null&&systemUser!=null){
					oaEmailSend.setSender(systemUser.getEmail());
					oaEmailSend.setBccreciever(systemUser.getEmail());
				}
			}
			
			List jobContacter=contractJobCvRecomService.getJobContacter((Integer)m.get("jobId"));
			
			
			
			//取推荐模板 
			SystemTemplate systemTemplate=systemTemplateService.getSystemTemplateByType(ConstantDic.EmailTemplateType_KI);
			if (systemTemplate!=null){
				Map vm=new HashMap();
				//设置标题
				vm.put("interviewDate", DateHelper.toString(oldContractJobCvInterview.getPlanDt(), DateHelper.DEFAULT_DATE_FORMAT));
				vm.put("interviewTime", DateHelper.toString(oldContractJobCvInterview.getPlanDt(), "HH:mm"));
				vm.put("jobName", (String)m.get("jobName"));
				vm.put("cvName", (String)cvMap.get("cvName"));
				
				vm.put("mobile", (String)cvMap.get("mobile"));
				vm.put("interviewPlace", oldContractJobCvInterview.getAddress());
				
				
				vm.put("interviewer", oldContractJobCvInterview.getInterviewer());
				vm.put("interviewerTitle", oldContractJobCvInterview.getInterviewerTitle());
				
				//查询面试官电话
				String sql="select * from (select d.mobile,d.tel from contract_job_cv_recom a,contract_job b,customer_contacter c,cv d where a.id=? and a.job_id=b.id and b.cust_id=c.cust_id and c.cv_id=d.id and d.cv_name=? ) t";
				 List values = new ArrayList();
				 values.add(oldContractJobCvInterview.getRecomId());
				 values.add(oldContractJobCvInterview.getInterviewer());
				List contact=systemUserService.getEntityDao().getJdbcDao().findByQuery(sql, values.toArray(), new MapObjectRowMapper());
				vm.put("interviewerPhone", "&nbsp;");
				if (contact!=null&&contact.size()>0){
					//取第一条记录的tel
					Map r=(Map)contact.get(0);
					if (ObjectUtils.isNotEmpty(r.get("tel")))
					  vm.put("interviewerPhone", r.get("tel"));
				}
				
				
				/*
				if (interviewSystemUser!=null) {
					vm.put("interviewer", interviewSystemUser.getName());
					vm.put("interviewerTitle", interviewSystemUser.getTitleName());
					//if (StringHelper.isEmpty(interviewSystemUser.getMobile()))						
					vm.put("interviewerPhone", ObjectUtils.toString(interviewSystemUser.getMobile())+" "+ObjectUtils.toString(interviewSystemUser.getTel()));
				}
				*/
				
				//if (systemUser!=null)vm.put("senderSign",systemUser.getName());
				if (systemUser!=null){
					//vm.put("senderSign",systemUser.getName());
					String sign=getSign(systemUser);
					vm.put("senderSign",sign);
				}
				oaEmailSend.setSubject(StringHelper.templateGen(vm, systemTemplate.getTitle()));
				//设置内容
				oaEmailSend.setContent(StringHelper.templateGen(vm, systemTemplate.getContent()));
				
				
				
			}
		}
		
		
		return oaEmailSend;
		
	}
	
	public OaEmailSend getOGEmail(ContractJobCvOffer contractJobCvOffer,
			OnlineUser onlineUser) {
		// TODO Auto-generated method stub
		
		OaEmailSend oaEmailSend=new OaEmailSend();
		
		
		ContractJobCvOffer oldContractJobCvOffer=null;
		List l=oaEmailSendDao.getHibernateTemplate().find("from ContractJobCvOffer t where t.id=?", contractJobCvOffer.getId());
		if (l!=null&&l.size()>0) oldContractJobCvOffer= (ContractJobCvOffer)l.get(0);
		
		
		ContractJobCvRecom contractJobCvRecom=null;
		if (oldContractJobCvOffer!=null){
			List recomList= contractJobCvRecomService.getContractJobCvRecomById(oldContractJobCvOffer.getRecomId());
			
			//取推荐模板 
			SystemTemplate systemTemplate=systemTemplateService.getSystemTemplateByType(ConstantDic.EmailTemplateType_OG);
			
			Map m=(Map)recomList.get(0);
			oaEmailSend.setCvName((String)m.get("cvName"));
			oaEmailSend.setJobName((String)m.get("jobName"));
			oaEmailSend.setAttachment((String)m.get("uploadDoc"));
			oaEmailSend.setAttachmentName((String)m.get("uploadDoc"));
			oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_OG);
			oaEmailSend.setCallId(contractJobCvOffer.getId());
			
			//设置发送人信息
			SystemUser systemUser=null;
			if (onlineUser!=null) {
				systemUser=systemUserService.findByUsername(onlineUser.getUsername());
				if (oaEmailSend!=null&&systemUser!=null){
					oaEmailSend.setSender(systemUser.getEmail());
					
				}
			}
			
			//查询职位联系人姓名和邮箱
			
			List jobContacter=contractJobCvRecomService.getJobContacter((Integer)m.get("jobId"));
			if (jobContacter!=null&&jobContacter.size()>0){
				
				Map mc=(Map)jobContacter.get(0);
				oaEmailSend.setReciever((String)mc.get("email"));
				oaEmailSend.setRecieverName((String)mc.get("cvName"));
			}
			
			if (systemTemplate!=null){
				Map vm=new HashMap();
				//设置标题
				vm.put("nowDate", DateHelper.toString(new Date(), DateHelper.DEFAULT_DATE_FORMAT));
				vm.put("jobName", oaEmailSend.getJobName());
				vm.put("cvName", oaEmailSend.getCvName());
				vm.put("jobContacter", oaEmailSend.getRecieverName());				
				//if (systemUser!=null)vm.put("senderSign",systemUser.getName());
				if (systemUser!=null){
					//vm.put("senderSign",systemUser.getName());
					String sign=getSign(systemUser);
					vm.put("senderSign",sign);
				}
				oaEmailSend.setSubject(StringHelper.templateGen(vm, systemTemplate.getTitle()));
				//设置内容
				oaEmailSend.setContent(StringHelper.templateGen(vm, systemTemplate.getContent()));
				
			}
		}
		
		
		return oaEmailSend;
		
		
	}
	public OaEmailSend getRTEmail(Integer jobId,Integer cvId,
			OnlineUser onlineUser) {
       OaEmailSend oaEmailSend=new OaEmailSend();
		
				
		
	     	ContractJobCvRecom oldContractJobCvRecom=null;
		
			List recomList=null;// contractJobCvRecomService.getContractJobCvRecomById(contractJobCvRecom.getId());
			
			if (recomList==null) return oaEmailSend;
			//取推荐模板 
			SystemTemplate systemTemplate=systemTemplateService.getSystemTemplateByType(ConstantDic.EmailTemplateType_RT);
			
			Map m=(Map)recomList.get(0);
			oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_RT);
			Map cvMap=cvService.getCvInfoById((Integer)m.get("cvId"));
			
			
			oaEmailSend.setCvName((String)m.get("cvName"));
			oaEmailSend.setJobName((String)m.get("jobName"));
			oaEmailSend.setAttachment((String)m.get("uploadDoc"));
			oaEmailSend.setAttachmentName((String)m.get("uploadDoc"));
			oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_RT);
			oaEmailSend.setCallId(0);
			oaEmailSend.setReciever((String)cvMap.get("email"));
			oaEmailSend.setRecieverName((String)cvMap.get("cvName"));
			
			//设置发送人信息
			SystemUser systemUser=null;
			if (onlineUser!=null) {
				systemUser=systemUserService.findByUsername(onlineUser.getUsername());
				if (oaEmailSend!=null&&systemUser!=null){
					oaEmailSend.setSender(systemUser.getEmail());
					oaEmailSend.setBccreciever(systemUser.getEmail());
				}
			}
			
			//查询职位联系人姓名和邮箱
			
//			List jobContacter=contractJobCvRecomService.getJobContacter((Integer)m.get("jobId"));
//			if (jobContacter!=null&&jobContacter.size()>0){
//				
//				Map mc=(Map)jobContacter.get(0);
//				oaEmailSend.setReciever((String)mc.get("email"));
//				oaEmailSend.setRecieverName((String)mc.get("cvName"));
//			}
			
			if (systemTemplate!=null){
				Map vm=new HashMap();
				//设置标题

				vm.put("jobName", oaEmailSend.getJobName());
				vm.put("cvName", oaEmailSend.getCvName());					
				//if (systemUser!=null)vm.put("senderSign",systemUser.getName());
				if (systemUser!=null){
					//vm.put("senderSign",systemUser.getName());
					String sign=getSign(systemUser);
					vm.put("senderSign",sign);
				}
				
				oaEmailSend.setSubject(StringHelper.templateGen(vm, systemTemplate.getTitle()));
				//设置内容
				oaEmailSend.setContent(StringHelper.templateGen(vm, systemTemplate.getContent()));
				
			}
		
		
		
		return oaEmailSend;
	}
	
	public OaEmailSend getRTEmail(ContractJobCvRecom contractJobCvRecom,
			OnlineUser onlineUser) {
       OaEmailSend oaEmailSend=new OaEmailSend();
		
				
		
	     	ContractJobCvRecom oldContractJobCvRecom=null;
		
			List recomList= contractJobCvRecomService.getContractJobCvRecomById(contractJobCvRecom.getId());
			
			if (recomList==null) return oaEmailSend;
			//取推荐模板 
			SystemTemplate systemTemplate=systemTemplateService.getSystemTemplateByType(ConstantDic.EmailTemplateType_RT);
			
			Map m=(Map)recomList.get(0);
			oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_RT);
			Map cvMap=cvService.getCvInfoById((Integer)m.get("cvId"));
			
			
			oaEmailSend.setCvName((String)m.get("cvName"));
			oaEmailSend.setJobName((String)m.get("jobName"));
			oaEmailSend.setAttachment((String)m.get("uploadDoc"));
			oaEmailSend.setAttachmentName((String)m.get("uploadDoc"));
			oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_RT);
			oaEmailSend.setCallId(contractJobCvRecom.getId());
			oaEmailSend.setReciever((String)cvMap.get("email"));
			oaEmailSend.setRecieverName((String)cvMap.get("cvName"));
			
			//设置发送人信息
			SystemUser systemUser=null;
			if (onlineUser!=null) {
				systemUser=systemUserService.findByUsername(onlineUser.getUsername());
				if (oaEmailSend!=null&&systemUser!=null){
					oaEmailSend.setSender(systemUser.getEmail());					
				}
			}
			
			//查询职位联系人姓名和邮箱
			
//			List jobContacter=contractJobCvRecomService.getJobContacter((Integer)m.get("jobId"));
//			if (jobContacter!=null&&jobContacter.size()>0){
//				
//				Map mc=(Map)jobContacter.get(0);
//				oaEmailSend.setReciever((String)mc.get("email"));
//				oaEmailSend.setRecieverName((String)mc.get("cvName"));
//			}
			
			if (systemTemplate!=null){
				Map vm=new HashMap();
				//设置标题

				vm.put("jobName", oaEmailSend.getJobName());
				vm.put("cvName", oaEmailSend.getCvName());					
				//if (systemUser!=null)vm.put("senderSign",systemUser.getName());
				if (systemUser!=null){
					//vm.put("senderSign",systemUser.getName());
					String sign=getSign(systemUser);
					vm.put("senderSign",sign);
				}
				oaEmailSend.setSubject(StringHelper.templateGen(vm, systemTemplate.getTitle()));
				//设置内容
				oaEmailSend.setContent(StringHelper.templateGen(vm, systemTemplate.getContent()));
				
			}
		
		
		
		return oaEmailSend;
	}
	
	public OaEmailSend getCIEmail(Customer customer,OnlineUser onlineUser) {
		// TODO Auto-generated method stub
		//
		OaEmailSend oaEmailSend=new OaEmailSend();
		
		Customer oldCustomer=null;
		List l=oaEmailSendDao.getHibernateTemplate().find("from Customer t where t.id=?", customer.getId());
		if (l!=null&&l.size()>0) oldCustomer= (Customer)l.get(0);
		
		Company company=null;
		if (oldCustomer!=null){
			l=oaEmailSendDao.getHibernateTemplate().find("from Company t where t.id=?", oldCustomer.getCompanyId());
			if (l!=null&&l.size()>0) company= (Company)l.get(0);
		}
		//去客户 职位联系人
		if (oldCustomer!=null){
			l=customerService.getCustomerContacter(oldCustomer.getId(), ConstantDic.CUSTOMER_CONTACTER_TYPE_JOB_CONTACTER);
			if (l!=null&&l.size()>0){
				Map mc=(Map)l.get(0);
				oaEmailSend.setReciever((String)mc.get("email"));
				oaEmailSend.setRecieverName((String)mc.get("cvName"));
			}
			
		}
		//设置发送人信息
		SystemUser systemUser=null;
		if (onlineUser!=null) {
			systemUser=systemUserService.findByUsername(onlineUser.getUsername());
			if (oaEmailSend!=null&&systemUser!=null){
				oaEmailSend.setSender(systemUser.getEmail());
				oaEmailSend.setBccreciever(systemUser.getEmail());
			}
		}
		//取推荐模板 
		SystemTemplate systemTemplate=systemTemplateService.getSystemTemplateByType(ConstantDic.EmailTemplateType_CI);
		oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_CI);
		if (systemTemplate!=null){
			Map vm=new HashMap();
			//设置标题	
			vm.put("jobContacter", oaEmailSend.getRecieverName());			
			//if (systemUser!=null)vm.put("senderSign",systemUser.getName());
			if (systemUser!=null){
				//vm.put("senderSign",systemUser.getName());
				String sign=getSign(systemUser);
				vm.put("senderSign",sign);
			}
			oaEmailSend.setSubject(StringHelper.templateGen(vm, systemTemplate.getTitle()));
			//设置内容
			oaEmailSend.setContent(StringHelper.templateGen(vm, systemTemplate.getContent()));
			
		}
		
		return oaEmailSend;
	}
	
	public OaEmailSend getJDEmail(Integer cvId,Integer jobId, OnlineUser onlineUser) {
		// TODO Auto-generated method stub
		ContractJob contractJob=(ContractJob)contractJobService.getById(jobId);
		
		Map cvMap=cvService.getCvInfoById(cvId);
		OaEmailSend oaEmailSend=new OaEmailSend();
		oaEmailSend.setReciever((String)cvMap.get("email"));
		oaEmailSend.setRecieverName((String)cvMap.get("cvName"));
		
		if (!StringHelper.isEmpty(contractJob.getJobFile())){
		  String jobfile=	contractJob.getJobFile();
		  String suffix = jobfile.substring(jobfile.lastIndexOf(".")+1);
		  String fileName= contractJob.getName()+"_"+DateHelper.toString(new Date(), "yyyyMMdd")+"."+suffix;
		  oaEmailSend.setAttachment(contractJob.getJobFile());
		  oaEmailSend.setAttachmentName(fileName);
		}
		
		oaEmailSend.setJobId(ObjectUtils.toString(jobId));
		oaEmailSend.setCvId(ObjectUtils.toString(cvId));
		
		//取推荐模板 
		SystemTemplate systemTemplate=systemTemplateService.getSystemTemplateByType(ConstantDic.EmailTemplateType_JD);
		
		//Map m=(Map)recomList.get(0);
		//oaEmailSend.setCvName((String)m.get("cvName"));
		//oaEmailSend.setJobName((String)m.get("jobName"));		
		oaEmailSend.setCallFrom(ConstantDic.EmailSourceType_JD);
		oaEmailSend.setCallId(0);
		
		//设置发送人信息
		SystemUser systemUser=null;
		if (onlineUser!=null) {
			systemUser=systemUserService.findByUsername(onlineUser.getUsername());
			if (oaEmailSend!=null&&systemUser!=null){
				oaEmailSend.setSender(systemUser.getEmail());
				oaEmailSend.setBccreciever(systemUser.getEmail());
			}
		}
		
		//查询职位联系人姓名和邮箱
		
		//oaEmailSend.setAttachment("/upload/JOBRECOM/20120430/2/edit.gif");
		
		if (systemTemplate!=null){
			Map vm=new HashMap();
			//设置标题	
			vm.put("cvName", (String)cvMap.get("cvName"));				
			if (contractJob!=null) vm.put("jobName", contractJob.getName());
			//if (systemUser!=null)vm.put("senderSign",systemUser.getName());
			if (systemUser!=null){
				//vm.put("senderSign",systemUser.getName());
				String sign=getSign(systemUser);
				vm.put("senderSign",sign);
			}
			oaEmailSend.setSubject(StringHelper.templateGen(vm, systemTemplate.getTitle()));
			//设置内容
			oaEmailSend.setContent(StringHelper.templateGen(vm, systemTemplate.getContent()));
			
		}
		
		
		return oaEmailSend;
	}
	
	public void saveFile(MultipartHttpServletRequest multipartRequest,
			MultipartFile multipartFile, OaEmailSend oaEmailSend,int type) {
		// TODO Auto-generated method stub
		 SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");     
		  
			//把公司id也加到目录里
		       String fileWebPathDir = Environment.UPLOADFILEDIR+"/"+SystemConstant.OASENDEMAIL+"/"+dateformat.format(new Date())+"/"+oaEmailSend.getCallFrom();      
		       /**得到文件保存目录的真实路径**/     
		       String fileRealPathDir = multipartRequest.getSession().getServletContext().getRealPath(fileWebPathDir);     
		       /**根据真实路径创建目录**/     
		       File fileSaveDir = new File(fileRealPathDir);     
		       if(!fileSaveDir.exists()) {    
		           fileSaveDir.mkdirs();     
		       }   
		       /**页面控件的文件流**/     
		       //MultipartFile multipartFile = multipartRequest.getFile("fileRes");      			       			      			       
		       /**上传文件名称**/     
		       
		          
		         
		       
		       /**获取文件的后缀**/     
		       String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")+1);     
		       String preName = multipartFile.getOriginalFilename().substring(0,multipartFile.getOriginalFilename().indexOf("."));
		       // 文件类型
		       
		       String uploadFileName =DateHelper.toString(new Date(), "HHmmss")+RandomHelper.createRandomString(3)+"."+suffix; // multipartFile.getOriginalFilename();//
		         
		       // 获取文件大小，以kB为单位   
		       long size = multipartFile.getSize();   
		       Double fSize =Math.round(size/1024.0*100)/100.0;   
		       String fileSize = Double.toString(fSize)+"KB";
		       if (fSize==0) fSize=new Double(1);
		       /**拼成完整的文件保存路径加文件**/     
		       String fileName = fileRealPathDir + File.separator   + uploadFileName;     
		       
		       File file = new File(fileName);      
		          
		       try {     
		           multipartFile.transferTo(file);  //将文件写入到指定的目录   
		       } catch (IllegalStateException e) {     
		           e.printStackTrace();     
		       } catch (IOException e) {            
		           e.printStackTrace();     
		       }    
		       //multipartFile.g
		      if (type==2) 
		      oaEmailSend.setAttachment2(fileWebPathDir+"/"+ uploadFileName);
		      else oaEmailSend.setAttachment(fileWebPathDir+"/"+ uploadFileName);
		      
		        
		       //contractJobCvRecom.setAttachmentFile(fileWebPathDir+"/"+ uploadFileName);
		       //如果没有填文件名称，则以文件名处理  特殊处理
		    //如果没有填文件名称，则以文件名处理
		       if (StringHelper.isEmpty(oaEmailSend.getAttachmentName())) oaEmailSend.setAttachmentName(preName);
		       
	}
	
	public OaEmailSend getJAEmailByMap(Map m) {
		// TODO Auto-generated method stub
		SystemTemplate systemTemplate=systemTemplateService.getSystemTemplateByType(ConstantDic.EmailTemplateType_JA);
		OaEmailSend oaEmailSend=new OaEmailSend();
		//oaEmailSend.setReciever((String)cvMap.get("email"));
		//oaEmailSend.setRecieverName((String)cvMap.get("cvName"));
		if (systemTemplate!=null){
			//Map vm=new HashMap();
			//设置标题	
			//vm.put("cvName", (String)cvMap.get("cvName"));				
			//if (contractJob!=null) vm.put("jobName", contractJob.getName());
			//if (systemUser!=null)vm.put("senderSign",systemUser.getName());
			oaEmailSend.setSubject(StringHelper.templateGen(m, systemTemplate.getTitle()));
			//设置内容
			oaEmailSend.setContent(StringHelper.templateGen(m, systemTemplate.getContent()));
			
		}
		return oaEmailSend;
	}
	@Override
	public boolean checkJobRecomSender(OaEmailSend oaEmailSend) {
		//同一个职位 同一个人选  只能一个人推荐
		return oaEmailSendDao.checkJobRecomSender(oaEmailSend);
	}
	
}
